package com.freemyleft.left.left_app.launcher;//package com.freemyleft.left.left_app.sign;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.freemyleft.left.left_app.R;
import com.freemyleft.left.left_app.R2;
import com.freemyleft.left.left_app.mian.index.BottomDelegate;
import com.freemyleft.left.left_app.sign.ISignListenter;
import com.freemyleft.left.zapp.app.AccountManager;
import com.freemyleft.left.zapp.delegates.LeftDelegate;
import com.freemyleft.left.zapp.net.RestClient;
import com.freemyleft.left.zapp.net.callback.ISuccess;
import com.freemyleft.left.zapp.util.LeftPreference;


import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by FreeMyleft on 2017/12/10.
 * //
 */

public class SignUpDelegate extends LeftDelegate {
    private ISignListenter mISignListener = null;
    private CountDownTimer mtTimer;
    TextView send_code;
    private boolean ispass;
    public final static String PHONE_PATTERN = "^1+[3578]+\\d{9}";//电话号正则
    public final static String PASSWORD_PATTERN = "^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{8,16}";//密码格式
    private String mcode;
    private String mPhone;
    @BindView(R2.id.phone_number)
    TextInputEditText phoneNumber;
    @BindView(R2.id.edit_code)
    TextInputEditText code;
    @BindView(R2.id.send_code)
    TextView sendCode;
    @BindView(R2.id.singin_btn)
    Button singinBtn;


    @OnClick(R2.id.send_code)
    void sendeCode() {
        mPhone = phoneNumber.getText().toString();
        if (Pattern.matches(PHONE_PATTERN, mPhone)) {
            phoneNumber.setError(null);
            mtTimer = new CountDownTimer(60000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    sendCode.setHint("请输入验证码");
                    sendCode.setTextColor(Color.parseColor("#777777"));
                    sendCode.setText(millisUntilFinished / 1000 + " s");
                    sendCode.setClickable(false);
                }

                @Override
                public void onFinish() {
                    sendCode.setText("重新发送");
                    sendCode.setClickable(true);
                }
            }.start();
            RestClient.builder().url("/home/login/sendsms")
                    .params("phone", mPhone)
                    .success(new ISuccess() {
                        @Override
                        public void onSuccess(String responnse) {
                            Log.e("TAG", responnse);
                            JSONObject obj = JSONObject.parseObject(responnse);
                            String sms = obj.getJSONObject("result").getString("sms");
                            String res_status = obj.getString("msg");
                            Log.e("TAG", res_status);
                            if (res_status.equals("1001")) {
                                Toast.makeText(getContext(), "发送失败，请重试", Toast.LENGTH_SHORT).show();
                                ispass = false;
                            } else if (res_status.equals("1000")) {
                                //成功返回 判断手机号和验证码是否一致
                                Toast.makeText(getContext(), "发送成功", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }).buid().post();
        } else {
            phoneNumber.setError("手机号码格式不正确");
        }
    }

    @OnClick(R2.id.singin_btn)
    void setSinginBtn() {
        mPhone = phoneNumber.getText().toString();
        mcode = code.getText().toString();
        Map<String, Object> prames = new HashMap<>();
        prames.put("phone", mPhone);
        prames.put("sms", mcode);

        RestClient.builder().url("/home/login/teacherlogin").loader(getContext())
                .params(prames)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String responnse) {
                        Log.e("code", responnse);
                        JSONObject object = JSONObject.parseObject(responnse);
                        String code = object.getString("code");
                        JSONObject result = object.getJSONObject("result");
                        String token = result.getString("token");
                        String teacherid = result.getString("id");
                        String msg = result.getString("msg");
                        String cityid = result.getString("cityid");
                        if (code.equals("1000")) {
                            LeftPreference.addCustomAppProfile("token", token);
                            LeftPreference.addCustomAppProfile("teacherid", teacherid);
                            LeftPreference.addCustomAppProfile("cityid", cityid);
                            start(new BottomDelegate(0));
                            AccountManager.setSignState(true);
                        } else if (code.equals("1002")) {
                            Toast.makeText(getContext(), "验证码错误", Toast.LENGTH_SHORT).show();
                        } else if (code.equals("1004")) {
                            Toast.makeText(getContext(), msg.toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }).buid().post();
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_signup;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ISignListenter) {
            mISignListener = (ISignListenter) activity;
        }
    }

}
